//
//  BIDApplyDetailHeaderView.m
//  JuanChengOA
//
//  Created by mal on 2017/7/28.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDApplyDetailHeaderView.h"
#import "BIDShowPhotoViewII.h"
#import "AppDelegate+StaticMethod.h"

@interface BIDApplyDetailHeaderView()<UIWebViewDelegate, BIDShowPhotoViewIIDelegate>

@end

@implementation BIDApplyDetailHeaderView
@synthesize delegate;

- (void)awakeFromNib
{
    [super awakeFromNib];
    _showPhotoView.delegate = self;
}
- (void)prepareWithDic:(NSDictionary *)dic
{
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGFloat height = [BIDGlobalMethods getHeightWithString:dic[@"title"] font:_titleLabel.font constraintSize:CGSizeMake(screenSize.width-16-8, MAXFLOAT)];
    _titleLabel.text = dic[@"title"];
    _heightForTitleLabelConstraint.constant = height;
    _nameLabel.text = [[NSString alloc] initWithFormat:@"%@ %@", dic[@"createUserName"], dic[@"createTime"]];
    //
    NSArray *arr = dic[@"files"];
    if(arr.count == 0)
    {
        _heightForShowPhotoViewConstraint.constant = 0;
    }
    else
    {
        NSMutableArray *imgPathArr = [NSMutableArray array];
        [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
            NSDictionary *tempDic = (NSDictionary *)obj;
            NSString *strImgPath = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], tempDic[@"path"]];
            [imgPathArr addObject:strImgPath];
        }];
        [_showPhotoView adjustLayoutWithImgPathArr:imgPathArr];
        _heightForShowPhotoViewConstraint.constant = CGRectGetHeight(_showPhotoView.frame);
    }
    //
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[dic[@"content"] dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    [_webView loadHTMLString:dic[@"content"] baseURL:nil];
    //
    [self setNeedsLayout];
    [self layoutIfNeeded];
    //
    CGRect frame = self.frame;
    frame.size.height = CGRectGetMaxY(_showPhotoView.frame)+8;
    self.frame = frame;
    [delegate updateHeaderView];
}

#pragma mark - BIDShowPhotoViewIIDelegate
- (void)showPhotoAtIndex:(NSInteger)index withImgArr:(NSArray*)imgArr;
{
    [delegate showPhotoAtIndex:index withImgArr:imgArr];
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    CGFloat webViewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
    webViewHeight = webView.scrollView.contentSize.height;
    _heightForWebViewConstraint.constant = webViewHeight;
    [self setNeedsLayout];
    [self layoutIfNeeded];
    //
    CGRect frame = self.frame;
    frame.size.height = CGRectGetMaxY(_showPhotoView.frame)+8;
    self.frame = frame;
    [delegate updateHeaderView];
}

@end
